package net.oschina.app.hjp1.hjpView;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.oschina.app.Myapp;
import net.oschina.app.R;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.hjp1.hjpAdapter.HjpAdapter;
import net.oschina.app.hjp1.hjpAdapter.RecycleViewDivider;
import net.oschina.app.hjpbean.DynamicFragmentBean.Tweet;
import net.oschina.app.hjpbean.DynamicFragmentBean.TweetsList;
import net.oschina.app.util.LogUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/7/15 0015.
 */

public class hjpFagment extends BaseFragment {

    private SwipeRefreshLayout sr;
    private RecyclerView rv;
    private View view;

    private List<Tweet> list;
    private HjpAdapter hjpAdapter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_default, container, false);
        sr = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_widget);
        rv = (RecyclerView) view.findViewById(R.id.rcv_news_zixun);


        return view;

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        LinearLayoutManager manager  = new LinearLayoutManager(getActivity());
        manager.setOrientation(RecyclerView.VERTICAL);
        rv.setLayoutManager(manager);

         rv.addItemDecoration(new RecycleViewDivider(
                Myapp.getContext(), LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.red)));


        Call<TweetsList> newTweetInfo = Myapp.shopService.getNewTweetInfo(0, 0, 20);
        newTweetInfo.enqueue(new Callback<TweetsList>() {
            @Override
            public void onResponse(Call<TweetsList> call, Response<TweetsList> response) {

                list = response.body().getList();

                LogUtils.d(list.get(0).getAuthor()+"" );
                Myapp.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        hjpAdapter = new HjpAdapter(list);
                        rv.setAdapter(hjpAdapter);

                    }
                });



            }

            @Override
            public void onFailure(Call<TweetsList> call, Throwable t) {
                t.printStackTrace();

            }
        });






    }




}
